﻿using System.Net.Mail;
using MammothMicro.SMTPGuard.Common;
using MammothMicro.SMTPGuard.Filter;

namespace MammothMicro.SmtpGuardFilter.Filter
{
    ///<summary>
    /// Handles email processing
    ///</summary>
    public class MailFilterFlow
    {
        /// <summary>
        /// Starts email processing
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool Execute(MailMessage email)
        {   
            // all emails are checked for virus
            if (FilterManager.UseClamWin)
            {
                if (!FilterManager.ClamWinFilter.Accept(email))
                    return false;
            }

            // white lists processing 
            if (FilterManager.TrustedIpFilter.Accept(email))
                return true;
            
            if (FilterManager.DomainWhiteListFilter.Accept(email))
                return true;
            
            if (FilterManager.EmailWhiteListFilter.Accept(email))
                return true;
        
            // black lists processing
            if (!FilterManager.BlockedEmailFilter.Accept(email))
                return false;
            
            if (!FilterManager.BlockedIpFilter.Accept(email))
                return false;
            
            if (!FilterManager.BlockedKeywordFilter.Accept(email))
                return false;
            
            //if (!FilterManager.LocalRecipientFilter.Accept(context))
            //    return false;
            
            if (FilterManager.BlockNullSender)
            {
                if (!FilterManager.NullSenderFilter.Accept(email))
                    return false;
            }

            if (FilterManager.BlockNullSubject)
            {
                if (!FilterManager.NullSubjectFilter.Accept(email))
                    return false; 
            }
           
            if (!FilterManager.RblFilter.Accept(email))
                return false;

            //if (!FilterManager.RulesFilter.Accept(email))
            //    return false;

            if (FilterManager.UseSpamAssasin)
            {
                if (!FilterManager.SpamAssasinFilter.Accept(email))
                    return false;
            }

            if (email.Attachments.Count > 0)
            {
                if (!FilterManager.AttachmentFilter.Accept(email))
                    return false; 
            }
             
            return true;

        }
    }
}


